<#assign tit>会籍合同销售</#assign>
<@page.html title=tit>
<script type="text/javascript" src="${_appPath!}/js/cal.js"></script>
<script type="text/javascript">
  function chgtp(t) {
    document.getElementById("tbd_old").style.display = t==1?"none":"";
    var fm = getEl("fm_input");
    fm.oldContractCode.value = "";
    fm.promotionId.value = "";
    setProdList([]);
    setOldContract(null);
    setReadOnly(fm.beginTime, t!=1);
    setReadOnly(fm.endTime, t!=2);
    getCardTypeList(t);
  }
  function getCardTypeList() {
    var fm = getEl("fm_input");
    var st = fm.saleType.value;
    var op = "";
    if(_oldmc && _oldmc.preferRenewal) op += "r";
    if(fm.promotionId.value=="1") op += "p";
    getJson("getCardTypeList?saleType="+st+"&op="+op, function(data){
      if(data.ret) {
        fillCardType(data.data);
      } else {
        window.alert(data.msg);
      }
    });
  }
  function fillCardType(tl) {
    var sl = getEl("sl_cardType");
    if(!sl) return;
    sl.length = 1;
    for(var i=0; i<tl.length; i++) {
      sl.options.add(new Option(tl[i].code+' - '+tl[i].name, tl[i].code));
    }
    setEarnestCardType();
  }

  function chgpm(id) {
    /*if(id) {
      getJson("getPromotion?id="+id, function(data){
        if(data.ret) {
          fillCardType(data.data.cardTypeList);
          setProdList(data.data.productList);
        } else {
          window.alert(data.msg);
        }
      });
    } else {
      setProdList([]);
      getCardTypeList(getEl("sl_saleType").value);
    }*/
    if(id) {
      getCardTypeList("5");
    } else {
      getCardTypeList(getEl("sl_saleType").value);
    }
  }
  function setProdList(ls) {
    var i, arr = [];
    arr.push('<table class="list">');
    for(i=0; i<ls.length; i++) {
      arr.push('<tr class="line'+((i%2)+1)+'">'
          + '<td width="80">'+ls[i].code+'</td>'
          + '<td width="180" class="left">'+ls[i].name+'</td>'
          + '<td></td>'
          + '</tr>')
    }
    arr.push('</table>')
    var div = getEl("div_prodlist");
    if(div) div.innerHTML = arr.join("");
  }

  var _cardInfo = null;
  var _fixMemberNum = 0;
  function chgct(c) {
    if(c!="") {
      var url = "getCardType?code="+c;
      var fm = getEl("fm_input");
      if(fm.promotionId.value!="") url += "&pmId="+fm.promotionId.value;
      if(fm.earnestId.value!="") url += "&earnestId="+fm.earnestId.value
      getJson(url, function(data){
        if(data.ret) {
          fillCardDetail(data.data);
        } else {
          window.alert(data.msg);
          fillCardDetail(null);
        }
      });
    } else {
      fillCardDetail(null);
    }

  }
  function fillCardDetail(card) {
    _cardInfo = card;
    var fm = getEl("fm_input");
    if(card==null) {
      fm.valid.value = "";
      fm.addValid.value = "";
      fm.beginTime.value = "";
      fm.endTime.value = "";
      fm.cardPrice.value = "";
      fm.totalPrice.value = "";
      fm.factPrice.value = "";
      fm.payAmount.value = "";
      setMemberNum(0);
      setShopList([],0);
    } else {
      fm.valid.value = card.valid;
      fm.addValid.value = card.addValid;
      var dt = new Date();
      if(fm.saleType.value=="2" && fm.status.checked) {//31升级
        dt = parseDate(fm.oldContractBegin.value);
      } else if(fm.saleType.value=="3" && _oldmc && _oldmc.contractEnd!="" && _oldmc.contractEnd>=getDateStr(new Date())) {//续会
        dt = parseDate(_oldmc.contractEnd);
        dt.addDay(1);
      }
      fm.beginTime.value = getDateStr(dt);
      fm.endTime.value = getDateStr(getEndTime(dt));
      fm.cardPrice.value = formatMoney(card.cardPrice);
      fm.totalPrice.value = formatMoney(card.cardPrice);
      fm.factPrice.value = "";
      fm.payAmount.value = "";
      setMemberNum(card.userNum);
      setShopList(card.shopList, card.manyShopQuantity);
    }
    var tr = getEl("tr_giftcard");
    if(tr) {
      fm.giftCardCode.value = "";
      if(card && card.giftCardType!=""/* && fm.saleType.value=="1"*/) {
        tr.cells[1].childNodes[2].setAttribute("info", "*赠送"+card.giftCardName);
        tr.style.display = "";
        chkGiftCardEnable();
      } else {
        tr.style.display = "none";
      }
    }
  }
  function chkGiftCardEnable() {
    var fm = getEl("fm_input");
    var mm = parseFloat(fm.factPrice.value);
    var mm2 = parseFloat(fm.totalPrice.value);
    var sp = getEl("sp_gift_info");
    if(isNaN(mm) || isNaN(mm2) || mm!=mm2) {
      fm.giftCardCode.value = "";
      setReadOnly(fm.giftCardCode, true);
      if(isNaN(mm))
        sp.innerHTML = "";
      else
        sp.innerHTML = "*售价非全额,不能赠送";
    } else {
      setReadOnly(fm.giftCardCode, false);
      sp.innerHTML = '<input class="opwin" value="..." onclick="selGiftCard();" type="button"> '+sp.getAttribute("info");
    }
  }

  function selGiftCard() {
    openWin("getGiftCardList", 500, 400);
  }
  function selGiftCardOk(vv) {
    var fm = getEl("fm_input");
    fm.giftCardCode.value = vv;
  }
  function chkGiftCard(ipt) {
    if(ipt.value!="") {
      getJson("getGiftCardNew?cardCode="+ipt.value, function(data){
        if(data.ret) {
          ipt.value = data.data.cardCode;
        } else {
          window.alert(data.msg);
        }
      });
    }
  }


  function setMemberNum(n) {
    var tb = getEl("tb_mbls");
    if(!tb) return;
    if(n<_fixMemberNum) n = _fixMemberNum;
    var i, tr, td;
    for(i=tb.rows.length-1; i>n; i--)
      tb.deleteRow(i);

    for(i=tb.rows.length-1; i<n; i++) {
      tr = tb.insertRow(-1);
      tr.className = "line"+((i%2)+1);
      td = tr.insertCell(-1);
      td.className = "no";
      td.innerHTML = (i+1);
      td = tr.insertCell(-1);
      td.style.whiteSpace = "nowrap";
      td.innerHTML = '<input style="text" value="" size="12" maxlength="16" onchange="chkCard(this);"><input type="button" class="opwin" value="..." onclick="selCard(this);"/>';
      td = tr.insertCell(-1);
      td.innerHTML = '<input type="text" value="" size="12" maxlength="50">';
      td = tr.insertCell(-1);
      td.innerHTML = '<select onchange="chkIdT(this);"><#if idType??><#list idType as t><option value="${t.id}">${t.name}</option></#list></#if></select>';
      td = tr.insertCell(-1);
      td.innerHTML = '<input style="text" value="" size="22" maxlength="18" onchange="chkIdN(this);">';
      td = tr.insertCell(-1);
      td.innerHTML = '<input type="checkbox">';
      td = tr.insertCell(-1);
      td.innerHTML = '<input type="hidden" value=""><span onclick="showUpload('+(i+1)+',1);"><input type="button" class="opwin" value="照片"/></span>';
      td = tr.insertCell(-1);
      td.innerHTML = '<input type="hidden" value=""><span onclick="showUpload('+(i+1)+',2);"><input type="button" class="opwin" value="证件"/></span>';
      td = tr.insertCell(-1);
      td.innerHTML = '<select><#if gender??><#list gender as t><option value="${t.id}">${t.name}</option></#list></#if></select>';
      td = tr.insertCell(-1);
      td.innerHTML = '<input style="text" value="" size="14" maxlength="11">';
    }
  }
  function addFixedMember(mb) {
    var tb = getEl("tb_mbls");
    if(!tb) return;
    var i, tr, td;
    for(i=tb.rows.length-1; i>0; i--)
      tb.deleteRow(i);

    _fixMemberNum = 1;
    tr = tb.insertRow(-1);
    tr.className = "line1";
    tr.mbCode = mb.memberCode;
    td = tr.insertCell(-1);
    td.className = "no";
    td.innerHTML = "1";
    td = tr.insertCell(-1);
    td.style.whiteSpace = "nowrap";
    td.innerHTML = '<input style="text" value="'+mb.cardCode+'" size="12" maxlength="16" onchange="chkCard(this);" ><input type="button" class="opwin" value="..." onclick="selCard(this);">';
    td = tr.insertCell(-1);
    td.innerHTML = '<input type="text" value="'+mb.memberName+'" size="12" maxlength="50" class="disable" readonly>';
    td = tr.insertCell(-1);
    td.innerHTML = mb.idNumber?('<input type="hidden" value="'+mb.idType+'">'+mb.idTypeName):('<select><#if idType??><#list idType as t><option value="${t.id}">${t.name}</option></#list></#if></select>');
    td = tr.insertCell(-1);
    td.innerHTML = '<input style="text" value="'+mb.idNumber+'" size="22" maxlength="18" '+(mb.idNumber?'class="disable" readonly':'')+'>';
    td = tr.insertCell(-1);
    td.innerHTML = '<input type="checkbox" checked>';
    td = tr.insertCell(-1);
    td.innerHTML = '<input type="hidden" value="'+mb.memberPhoto+'"><span'+(mb.memberPhoto?('><img src="${_picPath!}'+mb.memberPhoto+'" height="60">'):(' onclick="showUpload('+(i+1)+',1);"><input type="button" class="opwin" value="照片"/>'))+'</span>';
    td = tr.insertCell(-1);
    td.innerHTML = '<input type="hidden" value="'+mb.idCardPhoto+'"><span'+(mb.idCardPhoto?('><img src="${_appPath!}/images/idcard.png">'):(' onclick="showUpload('+(i+1)+',2);"><input type="button" class="opwin" value="证件"/>'))+'</span>';
    td = tr.insertCell(-1);
    td.innerHTML = '<input type="hidden" value="'+mb.gender+'">'+mb.genderName;
    td = tr.insertCell(-1);
    td.innerHTML = '<input style="text" value="'+mb.mobile+'" size="14" maxlength="11" '+(mb.mobile?'class="disable" readonly':'')+'>';
  }

  function setShopList(ls, n) {
    var i, arr = [];
    arr.push('<table class="list">');
    for(i=0; i<ls.length; i++) {
      arr.push('<tr class="line'+((i%2)+1)+'">'
          + '<td width="80">'+ls[i].code+'</td>'
          + '<td width="120" class="left">'+ls[i].name+'</td>'
          + '<td width="60"><input type="checkbox" name="shops" value="'+ls[i].code+'"></td>'
          + '<td></td>'
          + '</tr>')
    }
    arr.push('</table>');
    var div = getEl("div_shoplist");
    if(div) div.innerHTML = arr.join("");

    var sp = getEl("sp_shopsel");
    if(sp) {
      if(n>0)
        sp.innerHTML = '可任选'+n+'家门店';
      else if(ls.length>1)
        sp.innerHTML = '<label><input type="checkbox" onclick="selAllShop(this.checked);">全选 &nbsp; &nbsp;</label>';
      else
        sp.innerHTML = '';
    }
  }
  function selAllShop(v) {
    var cbs = document.getElementsByName("shops");
    if(cbs && cbs.length>0) {
      for(var i=0; i<cbs.length; i++)
        cbs[i].checked = v;
    }
  }

  function getEndTime(dt) {
    if(_cardInfo) {
      dt = parseDate(getDateStr(dt));
      if(_cardInfo.validUnit=='01')
        dt.addDay(_cardInfo.valids);
      else if(_cardInfo.validUnit=='02')
        dt.addWeek(_cardInfo.valids);
      else if(_cardInfo.validUnit=='03')
        dt.addMonth(_cardInfo.valids);
      else if(_cardInfo.validUnit=='04')
        dt.addYear(_cardInfo.valids);
    }
    return dt;
  }

  function setReadOnly(ipt, r) {
    ipt.readOnly = r;
    if(r && (!ipt.className || ipt.className.indexOf("disable")<0))
      ipt.className = (ipt.className?(ipt.className+" "):"")+"disable";
    if(!r && ipt.className && ipt.className.indexOf("disable")>=0)
      ipt.className = ipt.className.replace("disable", "");
  }

  function getcontr(c) {
    if(c!="") {
      getJson("getContract?code="+c, function(data){
        if(data.ret) {
          var mc = data.data;
          setOldContract(mc);
        } else {
          window.alert(data.msg);
          setOldContract(null);
        }
      });
    } else {
      setOldContract(null);
    }
  }
  var _oldmc = null;
  function setOldContract(mc) {
    var fm = getEl("fm_input");
    _fixMemberNum = 0;
    fm.cardType.value = "";
    fillCardDetail(null);
    setEarnestInfo(null);
    if(mc && !mc.idCardPhoto) {
      window.alert("无证件照，请先至审批流更改会员资料。");
      mc = null;
    } else if(mc && !mc.memberPhoto) {
      window.alert("资料不全，请先至审批流修改会员资料。");
      mc = null;
    }
    _oldmc = mc;
    if(mc) {
      fm.oldContractCode.value = mc.contractCode;
      fm.memberCode.value = mc.memberCode;
      fm.oldContractType.value = mc.cardType+" - "+mc.cardTypeName;
      fm.oldContractBegin.value = getDateStr(new Date(mc.beginTime));
      fm.oldContractEnd.value = getDateStr(new Date(mc.endTime));
      fm.oldContractAmount.value = formatMoney(mc.factPrice);
      fm.inAmount.value = formatMoney(mc.leftAmount);
      fm.status.checked = false;
      getEl("lb_sj31").style.display = (fm.saleType.value==2 && mc.beginDays<31)?"":"none";
      addFixedMember(mc);
      if(fm.saleType.value==3)
        getCardTypeList(mc.preferRenewal?4:3);
    } else {
      //fm.oldContractCode.value = "";
      fm.memberCode.value = "";
      fm.oldContractType.value = "";
      fm.oldContractBegin.value = "";
      fm.oldContractEnd.value = "";
      fm.oldContractAmount.value = "";
      fm.inAmount.value = "";
      fm.status.checked = false;
      getEl("lb_sj31").style.display = "none";
    }
  }
  function chgsj31(b) {
    var fm = getEl("fm_input");
    if(b) {
      fm.inAmount.value = fm.oldContractAmount.value;
    } else {
      fm.inAmount.value = _oldmc?formatMoney(_oldmc.leftAmount):"";
    }

    if(_cardInfo) {
      var dt = new Date();
      if(b) dt = parseDate(fm.oldContractBegin.value);
      fm.beginTime.value = getDateStr(dt);
      fm.endTime.value = getDateStr(getEndTime(dt));
      fm.factPrice.value = "";
      fm.payAmount.value = "";
      chkGiftCardEnable();
    }
  }

  function seldj() {
    var url = "getEarnestList?type=1";
    var fm = getEl("fm_input");
    if(fm.saleType.value!="1") {
      if(fm.memberCode.value=="") {
        window.alert("请先输入原合同编号!");
        return;
      }
      url += "&memberCode="+fm.memberCode.value;
    }
    openWin(url, 620, 400);
  }
  function seldjOk(vv) {
    var s = vv.split(",");
    getJson("getEarnest?id="+s[0], function(data){
      if(data.ret) {
        setEarnestInfo(data.data);
      } else {
        window.alert(data.msg);
        setEarnestInfo(null);
      }
    });
  }
  var _esInfo = null;
  function setEarnestInfo(es) {
    var fm = getEl("fm_input");
    if(es) {
      _esInfo = es;
      fm.earnestId.value = es.saleId;
      fm.earnestAmount.value = es.payAmount;
      if(fm.saleType.value=="1") addFixedMember(es);
      setEarnestCardType();
    } else {
      _esInfo = null;
      fm.earnestId.value = "";
      fm.earnestAmount.value = "";
    }
  }
  function setEarnestCardType() {
    var fm = getEl("fm_input");
    var es = _esInfo;
    if(es && es.cardType) {
      //fm.cardType.value = es.cardType;
      var sl = fm.cardType;
      var i, sid = -1;
      for(i=1; i<sl.options.length; i++) {
        if(sl.options[i].value==es.cardType) {
          sid = i;
          break;
        }
      }
      if(sid==-1 && es.overDueUse==0) {
        sl.options.add(new Option(es.cardType+' - '+es.cardTypeName, es.cardType));
        sid = sl.options.length-1;
      }
      if(sid!=-1) sl.selectedIndex = sid;
    }
    chgct(fm.cardType.value);
  }

  var _slc;
  function selCard(ipt) {
    _slc = ipt.previousSibling;
    openWin("getCardList", 500, 400);
  }
  function selCardOk(vv) {
    _slc.value = vv;
  }
  function chkCard(ipt) {
    if(ipt.value!="") {
      getJson("getCardNew?cardCode="+ipt.value, function(data){
        if(data.ret) {
          ipt.value = data.data.cardCode;
        } else {
          window.alert(data.msg);
        }
      });
    }
  }

  function chkIdT(ipt) {
    var idtp = ipt.value;
    var idn = ipt.parentNode.nextSibling.firstChild.value;
    chkIdTN(idtp, idn);
  }
  function chkIdN(ipt) {
    var idtp = ipt.parentNode.previousSibling.firstChild.value;
    var idn = ipt.value;
    chkIdTN(idtp, idn);
  }
  function chkIdTN(idtp, idn) {
    if(idtp!="" && idn!="") {
      if(idtp=="1" && !isIdNumber(idn)) {
        window.alert("请输入有效的身份证号码");
        return;
      }
      getJson("getIdNumberChk?idt="+idtp+"&idn="+idn, function(data){
        if(!data.ret) {
          window.alert(data.msg);
        }
      });
    }
  }

  function chgfact() {
    var fm = getEl("fm_input");
    if(fm.factPrice.value=="") {
      fm.payAmount.value = "";
      return;
    }
    var m1 = parseFloat(fm.factPrice.value);
    if(isNaN(m1) || m1<0) {
      window.alert("金额错误");
      return;
    }
    var m2 = 0;
    if(fm.earnestAmount.value!="") m2 = parseFloat(fm.earnestAmount.value);
    var m3 = 0;
    if(fm.saleType.value=="2" && fm.inAmount.value!="") m3 = parseFloat(fm.inAmount.value);
    var pym = m1-m2-m3;
    if(pym<0) pym = 0;
    fm.factPrice.value = formatMoney(m1);
    fm.payAmount.value = formatMoney(pym);
    chkGiftCardEnable();
  }

  function chgpaym() {
    var fm = getEl("fm_input");
    if(fm.payAmount.value=="") {
      fm.factPrice.value = "";
      return;
    }
    var m1 = parseFloat(fm.payAmount.value);
    if(isNaN(m1) || m1<0) {
      window.alert("金额错误");
      return;
    }
    var m2 = 0;
    if(fm.earnestAmount.value!="") m2 = parseFloat(fm.earnestAmount.value);
    var m3 = 0;
    if(fm.saleType.value=="2" && fm.inAmount.value!="") m3 = parseFloat(fm.inAmount.value);
    fm.factPrice.value = formatMoney(m1+m2+m3);
    fm.payAmount.value = formatMoney(m1);
    chkGiftCardEnable();
  }

  function chgdt1() {
    var fm = getEl("fm_input");
    var dt0 = getDateStr(new Date());
    var dt = parseDate(fm.beginTime.value);
    if(dt==null) {
      fm.beginTime.value = dt0;
    } else if(fm.beginTime.value<dt0) {
      window.alert("开始日期不能早于今天");
      fm.beginTime.value = dt0;
    } else if(_cardInfo) {
      var dt1 = new Date();
      dt1.addDay(_cardInfo.openPeriod);
      dt1 = getDateStr(dt1);
      if(fm.beginTime.value>dt1) {
        window.alert("开始日期不能晚于"+dt1);
        fm.beginTime.value = dt1;
      }
    }
    dt = parseDate(fm.beginTime.value);
    fm.endTime.value = getDateStr(getEndTime(dt));
  }

  function chgdt2() {
    if(!_cardInfo) return;
    var fm = getEl("fm_input");
    var dt = parseDate(fm.beginTime.value);
    var dt2 = parseDate(fm.endTime.value);
    if(dt==null || dt2==null) return
    var dd0 = getEndTime(dt).getTime()-dt.getTime();
    var dd = dt2.getTime()-dt.getTime();
    var mm = _cardInfo.cardPrice * dd / dd0;
    fm.totalPrice.value = formatMoney(mm);
    chkGiftCardEnable();
  }

  function save(fm) {
    if(!fm) fm = getEl("fm_input");
    if(!_cardInfo) {
      window.alert("请选择会籍类型");
      return false;
    }
    if(_cardInfo.manyShopQuantity>0) {
      var cbsh = document.getElementsByName("shops");
      var nsl = 0;
      for(var i=0; cbsh && i<cbsh.length; i++)
        if(cbsh[i].checked) nsl++;
      if(nsl==0) {
        window.alert("请选择可使用门店");
        return false;
      }
      if(nsl>_cardInfo.manyShopQuantity) {
        window.alert("最多只能选择"+_cardInfo.manyShopQuantity+"家门店");
        return false;
      }
    }
    if(fm.factPrice.value=="") {
      window.alert("请输入实际售价");
      return false;
    }
    var pym = parseFloat(fm.payAmount.value);
    if(isNaN(pym) || pym<0) {
      window.alert("应收金额错误");
      return;
    }
    var mm = parseFloat(fm.factPrice.value);
    var mm2 = parseFloat(fm.totalPrice.value);
    if(mm!=mm2) {
      if(fm.saleType.value=="1") {
        window.alert("实际售价不符");
        return false;
      } else {
        if(!window.confirm("实际售价不符,需要审批?")) return false;
      }
    }
    if(/*fm.saleType.value=="1"*/mm==mm2 && _cardInfo.giftCardType!="") {
      if(fm.giftCardCode.value=="" || fm.giftCardCode.value.length!=19) {
        window.alert("请输入19位的储值卡号");
        return false;
      }
      /*if(isNaN(fm.giftCardCode.value)) {
        window.alert("请输入储值卡后6位数字");
        return false;
      }*/
    }
    if(!getmbls()) return false;
    //openWin("getPay?money="+pym, 500, 400);
    //添加短信验证
    var tb = getEl("tb_mbls");
    var mobile = tb.rows[1].cells[9].firstChild.value;
    if(mobile==fm.verifyMobile.value && (fm.verifyStatus.value=="1" || (fm.verifyStatus.value=="2" && fm.managerCode.value!=""))) {
      openWin("getPay?money="+pym, 500, 400);
    } else {
      getEl("mobile").value = mobile;
      showMbvf(true);
    }
    return false;
  }
  function getmbls() {
    var tb = getEl("tb_mbls");
    if(!tb) return false;
    if(tb.rows.length<2) {
      window.alert("请输入会员信息");
      return false;
    }
    var i, j, ss, tr, arr = new Array();
    var nn = [1,2,8,3,4,9,6,7];
    var cc = ["卡号","会员姓名","性别","证件类型","证件号","手机","照片","证件照"]
    for(i=1; i<tb.rows.length; i++) {
      tr = tb.rows[i];
      for(j=0; j<nn.length; j++) {
        ss = tr.cells[nn[j]].firstChild.value;
        if(ss=="") {
          window.alert("请输入"+cc[j]);
          return false;
        }
      }
      //校验身份证
      if(tr.cells[3].firstChild.value=="1" && !tr.cells[4].firstChild.readOnly && !isIdNumber(tr.cells[4].firstChild.value)) {
        window.alert("请输入有效的身份证号码");
        return false;
      }
      ss = (tr.mbCode||"")+",";
      for(j=0; j<nn.length; j++) {
        ss += tr.cells[nn[j]].firstChild.value+",";
      }
      if(tr.cells[5].firstChild.checked) ss += "1";
      arr.push(ss);
    }
    var fm = getEl("fm_input");
    fm.mbls.value = arr.join("|");
    return true;
  }
  function payOk(vv) {
    var fm = getEl("fm_input");
    fm.payls.value = vv;
    postForm(fm.action, fm, saveRet);
  }
  function saveRet(data) {
    window.alert(data.msg);
    if(data.ret) {
      //getEl("fm_input").saleType.value = "1";
      //chgtp(1);
      cload();
    }
  }

  var _upIdx = 0;
  function showUpload(n, tp) {
    _upIdx = n;
    if(tp==2)
      openWin("upload?tp="+tp, 500, 400);
    else
      openWin("upload?tp="+tp, 650, 450);
  }
  function uploadOk(upl) {
    var tb = getEl("tb_mbls");
    if(!tb || !tb.rows[_upIdx]) return;
    var td = tb.rows[_upIdx].cells[6];
    td.childNodes[0].value = upl.furl;
    td.childNodes[1].innerHTML = '<img src="${_picPath!}'+upl.furl+'" height="60">';
  }
  function uploadOk2(upl) {
    var tb = getEl("tb_mbls");
    if(!tb || !tb.rows[_upIdx]) return;
    var td = tb.rows[_upIdx].cells[7];
    td.childNodes[0].value = upl.furl;
    td.childNodes[1].innerHTML = '<img src="${_appPath!}/images/idcard.png">';
  }

  function chkClose() {
    var div = getEl("div_openwin");
    if(div) {
      var w = div.firstChild.childNodes[1].firstChild.contentWindow;
      if(w && w.chkClose) return w.chkClose();
    }
    return true;
  }


  function showMbvf(b) {
    getEl("div_fullbg").style.display = b?"":"none";
    getEl("div_mbvf").style.display = b?"":"none";
  }

  function showMgpw(b) {
    getEl("div_fullbg").style.display = b?"":"none";
    getEl("div_mgpw").style.display = b?"":"none";
  }

  var _leftSec = 0;
  function sendyz() {
    var mobile = getEl("mobile").value;
    if(!isMobile(mobile)) {
      window.alert("请输入正确的手机号！");
      return;
    }
    getJson("mobileVerify?mobile="+mobile, function(data){
      window.alert(data.msg);
      if(data.ret) {
        _leftSec = 60;
        chkLc();
      }
    })
  }
  function chkLc() {
    //if(_leftSec<=0) return;
    _leftSec--;
    var bt = getEl("btSendyz");
    if(_leftSec>0) {
      bt.value = _leftSec+"秒后重新获取";
      bt.disabled = true;
      window.setTimeout(chkLc, 1000);
    } else {
      bt.value = "获取短信验证码";
      bt.disabled = false;
    }
  }

  function chkyz() {
    var mobile = getEl("mobile").value;
    var code = getEl("chkcode").value;
    if(code.length!=6) {
      window.alert("请输入手机收到的6位验证码！");
      return;
    }
    postJson("_mobileVerify", "mobile="+mobile+"&code="+code, function(data){
      window.alert(data.msg);
      if(data.ret) {
        var fm = getEl("fm_input");
        fm.verifyMobile.value = mobile;
        fm.verifyStatus.value = 1;
        fm.managerCode.value = "";
        showMbvf(false);
        var pym = parseFloat(fm.payAmount.value);
        openWin("getPay?money="+pym, 500, 400);
      }
    });
  }

  function chgpw() {
    showMbvf(false);
    showMgpw(true);
  }

  function chkpw() {
    var mobile = getEl("mobile").value;
    var code = getEl("mgCode").value;
    var pwd = getEl("mgPwd").value;
    if(code=="") {
      window.alert("请输入运营经理帐号！");
      return;
    }
    if(pwd=="") {
      window.alert("请输入密码！");
      return;
    }
    postJson("_managerConfirm", "code="+code+"&pwd="+pwd, function(data){
      window.alert(data.msg);
      if(data.ret) {
        var fm = getEl("fm_input");
        fm.verifyMobile.value = mobile;
        fm.verifyStatus.value = 2;
        fm.managerCode.value = code;
        showMgpw(false);
        var pym = parseFloat(fm.payAmount.value);
        openWin("getPay?money="+pym, 500, 400);
      }
    })
  }
</script>
<div style="width:1000px;">

  <form id="fm_input" method="post" action="_add" onsubmit="return save(this);">
    <input type="hidden" name="payls" value=""><input type="hidden" name="mbls" value="">
    <input type="hidden" name="verifyMobile" value=""><input type="hidden" name="verifyStatus" value=""><input type="hidden" name="managerCode" value="">
    <div class="kbox">
      <table class="input w4" style="float:left;">
        <tr>
          <th width="85">销售类型</th>
          <td width="160"><select name="saleType" id="sl_saleType" onchange="chgtp(this.value);">
            <option value="1">1-新合同</option>
            <option value="2">2-升级</option>
            <option value="3">3-续会</option>
          </select></td>
          <th width="85"></th>
          <td width="160"><!--label><input type="checkbox" class="cb" name="signStatus" value="0">不使用手写签名</label--></td>
        </tr>
        <tbody id="tbd_old" style="display:none; border:3px solid #ffefce;">
        <tr>
          <th>原合同编号</th>
          <td><input type="text" name="oldContractCode" value="" onchange="getcontr(this.value);" maxlength="20"></td>
          <th></th>
          <td><input type="hidden" name="memberCode" value=""><label id="lb_sj31"><input type="checkbox" name="status" value="2" onclick="chgsj31(this.checked);">31天内升级</label></td>
        </tr>
        <tr>
          <th>原会籍类型</th>
          <td colspan="3"><input type="text" name="oldContractType" value="" class="w2 disable" readonly></td>
        </tr>
        <tr>
          <th>原合同起日</th>
          <td><input type="text" name="oldContractBegin" value="" class="date disable" maxlength="10" readonly></td>
          <th>原合同迄日</th>
          <td><input type="text" name="oldContractEnd" value="" class="date disable" maxlength="10" readonly></td>
        </tr>
        <tr>
          <th>原合同价值</th>
          <td><input type="text" name="oldContractAmount" value="" class="disable" readonly></td>
          <th>剩余价值</th>
          <td><input type="text" name="inAmount" value="" class="disable" readonly></td>
        </tr>
        </tbody>
        <tr>
          <th>定金编号</th>
          <td><input type="text" name="earnestId" value="" style="width:90px;" class="disable" readonly>
            <input type="button" class="opwin" value="..." onclick="seldj();"/></td>
          <th>定金金额</th>
          <td><input type="text" name="earnestAmount" value="" class="disable" readonly></td>
        </tr>
        <tr>
          <th>营销活动</th>
          <!--td colspan="3"><select name="promotionId" onchange="chgpm(this.value);" class="w2"><option value="">--请选择--</option><#if promotionList??><#list promotionList as t><option value="${t.promotionId}">${t.chName}</option></#list></#if></select></td-->
          <td colspan="3"><select name="promotionId" onchange="chgpm(this.value);" class="w2"><option value="">不参加营销活动</option><option value="1">参加营销活动</option></select></td>
        </tr>
        <tr>
          <th>会籍类型</th>
          <td colspan="3"><select name="cardType" id="sl_cardType" onchange="chgct(this.value);" class="w2"><option value="">--请选择--</option></select></td>
        </tr>
        <tr>
          <th>有效期</th>
          <td><input type="text" name="valid" value="" class="disable" readonly></td>
          <th>赠送天数</th>
          <td><input type="text" name="addValid" value="" class="disable" readonly></td>
        </tr>

        <tr>
          <th>开始日期</th>
          <td><input type="text" name="beginTime" value="" class="date" maxlength="10" onchange="chgdt1();" onfocus="if(!this.readOnly)cal(this);"></td>
          <th>结束日期</th>
          <td><input type="text" name="endTime" value="" class="date disable" maxlength="10" onchange="chgdt2();" onfocus="if(!this.readOnly)cal(this);" readonly></td>
        </tr>
        <tr>
          <th>指导价</th>
          <td><input type="text" name="cardPrice" value="" class="disable" readonly></td>
          <th>总价</th>
          <td><input type="text" name="totalPrice" value="" class="disable" readonly></td>
        </tr>
        <tr>
          <th>实际售价</th>
          <td><input type="text" name="factPrice" value="" onchange="chgfact();"></td>
          <th>应收金额</th>
          <td><input type="text" name="payAmount" value="" onchange="chgpaym();"></td>
        </tr>

        <tr id="tr_giftcard" style="display:none;">
          <th>储值卡卡号</th>
          <td colspan="3"><span></span><input type="text" name="giftCardCode" value="" onchange="chkGiftCard(this);" style="width:180px;" maxlength="19"><span id="sp_gift_info" style="padding-left:10px;">输入赠送的储值卡卡号</span></td>
        </tr>
        <tr>
          <th>备注</th>
          <td colspan="3"><textarea name="note"></textarea></td>
        </tr>
      </table>

      <div class="kbox" style="float:left; margin-left:10px; width:320px; padding:0 20px 20px;">
        <div class="lsinfo">您选择的营销活动附带赠品信息</div>
        <table class="list">
          <tr>
            <th width="80">编号</th>
            <th width="180" class="left">名称</th>
            <th></th>
          </tr>
        </table>
        <div id="div_prodlist" style="max-height:156px; overflow-y:auto;"></div>

        <div class="lsinfo"><span id="sp_shopsel" style="float:right; margin-right:20px;"></span>可使用门店列表</div>
        <table class="list">
          <tr>
            <th width="80">门店</th>
            <th width="120" class="left">描述</th>
            <th width="60">自选门店</th>
            <th></th>
          </tr>
        </table>
        <div id="div_shoplist" style="max-height:156px; overflow-y:auto;">
          <!--table class="list">
            <tr class="line1">
              <td width="80">SH101</td>
              <td width="120" class="left">中山店</td>
              <td width="60"><input type="checkbox" name="shops" value="SH101"></td>
              <td></td>
            </tr>
          </table-->
        </div>
      </div>

      <div class="clear"></div>
      <div class="lsinfo">卡号列表</div>
      <table class="list trans" id="tb_mbls" style="width:900px;">
        <tr>
          <th width="6%" nowrap>序号</th>
          <th width="15%">卡号</th>
          <th width="12%">会员姓名</th>
          <th width="10%">证件类型</th>
          <th width="15%">证件号</th>
          <th width="5%" nowrap>签订人</th>
          <th width="8%">照片</th>
          <th width="7%">证件照</th>
          <th width="8%">性别</th>
          <th width="12%">手机</th>
        </tr>
        <!--tr class="line1">
          <td class="no">1</td>
          <td><input style="text" value="K829832938" size="14" maxlength="16"></td>
          <td><input type="text" value="胡艳艳" size="12" maxlength="16"></td>
          <td><select><#if idType??><#list idType as t><option value="${t.id}">${t.name}</option></#list></#if></select></td>
          <td><input style="text" value="310010199001011234" size="22" maxlength="18"></td>
          <td><input type="checkbox"></td>
          <td><img src="images/111.jpg" height="50"></td>
          <td><select><#if gender??><#list gender as t><option value="${t.id}">${t.name}</option></#list></#if></select></td>
          <td><input style="text" value="13030303030" size="14" maxlength="11"></td>
        </tr>
        <tr class="line2">
          <td>K829832938</td>
          <td>胡艳艳</td>
          <td>身份证</td>
          <td>310010199001011234</td>
          <td class="sales">销售人</td>
          <td><img src="images/111.jpg" height="50"></td>
          <td>男</td>
          <td>13030303030</td>
        </tr-->
      </table>
    </div>
    <div class="button"><input type="button" value="确认并提交" onclick="save();"></div>
  </form>
</div>
<div id="div_mbvf" class="openwin" style="display:none; width:440px; margin-left:-220px; margin-top:-110px;">
  <dl><dt><span>手机验证</span><a href="javascript:showMbvf(false);">×</a></dt>
    <dd><div class="kbox">
      <table class="input">
        <tr><th>手机号：</th><td><input type="text" id="mobile" size="16" maxlength="11" readonly class="disable" /></td>
          <td><input type="button" class="opwin" id="btSendyz" value="获取短信验证码" onclick="sendyz();"/></td></tr>
        <tr><th>验证码：</th><td><input type="text" id="chkcode" size="10" maxlength="6" /><span></span></td></tr>
      </table>
      <div class="button" style="margin:10px 0 0;"><input type="button" value=" 确 定 " onclick="chkyz();"/>
        <#if canManagerconfirm??><input type="button" value="营运经理确认" onclick="chgpw();"/></#if></div>
    </div></dd></dl>
</div>
<div id="div_mgpw" class="openwin" style="display:none; width:340px; margin-left:-170px; margin-top:-110px;">
  <dl><dt><span>输入营运经理帐号确认</span><a href="javascript:showMgpw(false);">×</a></dt>
    <dd><div class="kbox">
      <table class="input">
        <tr><th width="70">帐号：</th><td><input type="text" id="mgCode" size="16" maxlength="20" /></td></tr>
        <tr><th>密码：</th><td><input type="password" id="mgPwd" size="16" maxlength="20" /></td></tr>
      </table>
      <div class="button" style="margin:10px 0 0;"><input type="button" value=" 确 定 " onclick="chkpw();"/></div>
    </div></dd></dl>
</div>
<script type="text/javascript">chgtp(getEl("sl_saleType").value);</script>
</@page.html>